package view.WelcomePanels.SubPanels.MainButtonPanels;

import domain.Client;
import domain.DropboxController;
import domain.WelcomeController;
import dto.LoginDTO;
import dto.WelcomeDTO;
import java.awt.event.ActionEvent;
import java.net.ConnectException;
import javax.swing.JOptionPane;
import view.DropboxFrame;
import view.WelcomeFrame;

/**
 *
 * @author Joachim
 */
public class LoginButtonPanel extends MainButtonPanel {
    
    private WelcomeFrame frame;
    
    public LoginButtonPanel(WelcomeDTO dto, WelcomeController controller, WelcomeFrame frame) {
        super(dto, controller, "Log in");
        this.frame = frame;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            boolean loginSuccess = super.controller.logIn((LoginDTO)super.dto);
            if(loginSuccess) {
                JOptionPane.showMessageDialog(null, "Login successful.");
                frame.dispose();
                new DropboxFrame(new DropboxController(Client.getInstance()));
            }
            else {
                JOptionPane.showMessageDialog(null, "Login failed.");
            }
        }
        catch(IllegalArgumentException iae) {
            JOptionPane.showMessageDialog(null, iae.getMessage());
        }
        catch(ConnectException ce) {
            JOptionPane.showMessageDialog(null, ce.getMessage());
        }
    }
    
}
